import toolMgr from '@/biz/base/tool.js'
import dt from '@dt/dt'

let _this = null

function accessory(info) {
  let files = []
  if (info.fileAttachList) {
    files = info.fileAttachList.map(item => {
      return {
        value: item.accessoryName,
        key: item.accessoryUrl
      }
    })
  }
  return [{
    title: '文件附件',
    tag: {
      closable: false,
      options: files,
      color: 'blue',
      click: (tag) => {
        if ((_this.orderType == '认购' && _this.unlocking('0104010406')) ||
          (_this.orderType == '签约' && _this.unlocking('0104030406'))) {
          toolMgr.downloadFile(tag.key, tag.value)
        } else {
          _this.$Message.info('无下载权限！')
          return false;
        }
      }
    }
  }]
}

function orderPic(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.imageAttachList && info.imageAttachList.length > 0) {
    info.imageAttachList.forEach(item => {
      if (item) {
        item.url = item.accessoryUrl
        files.push({
          name: item.accessoryName,
          url: item.accessoryUrl,
        })
      }
    })
  }

  return {
    title: '附件',
    key: 'imageAttachList',
    uploadImg: {
      title: '附件',
      api,
      files,
      multiple: false,
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      },
      show: false,
      hideDel: true,
      maxlength: 0,
      showDownLoad: (_this.orderType == '认购' && _this.unlocking('0104010406')) || (_this
        .orderType == '签约' && _this.unlocking('0104030406')) ? true : false,
    }
  }
}

function part1() {
  return [
    [{
      title: '调整订单数',
      key: 'adjustCount',
      text: {},
      data: {
        from: (info) => {
          return info.adjustCount + '个'
        }
      }
    }],
    [{
      title: '调整前总面积',
      key: 'beforeAdjustArea',
      text: {},
      data: {
        from: (info) => {
          return info.beforeAdjustArea + '㎡'
        }
      }
    }, {
      title: '调整后总面积',
      key: 'afterAdjustArea',
      text: {},
      data: {
        from: (info) => {
          return info.afterAdjustArea + '㎡'
        }
      }
    }, {
      title: '调整面积差',
      key: 'areaDV',
      text: {
        tooltip: false
      },
      data: {
        from: (info) => {
          let color = info.areaDV > 0 ? '#D9001B' : info.areaDV < 0 ? '#70B603' :
            '#515a6e'
          let areaDVTmp = info.areaDV > 0 ? '+' + info.areaDV : info
            .areaDV < 0 ? info.areaDV : info.areaDV
          return `<span style=color:${color};margin-left:5px>${areaDVTmp}㎡</span>`
        }
      }
    }],
    [{
      title: `调整前${_this.orderType}总价`,
      key: 'beforeAdjustPrice',
      text: {},
      data: {
        from: (info) => {
          return info.beforeAdjustPrice + '元'
        }
      }
    }, {
      title: `调整后${_this.orderType}总价`,
      key: 'afterAdjustPrice',
      text: {},
      data: {
        from: (info) => {
          return info.afterAdjustPrice + '元'
        }
      }
    }, {
      title: '调整总价差',
      key: 'priceDV',
      text: {
        tooltip: false
      },
      data: {
        from: (info) => {
          let color = info.priceDV > 0 ? '#D9001B' : info.priceDV < 0 ? '#70B603' :
            '#515a6e'
          let priceDVTmp = info.priceDV > 0 ? '+' + info.priceDV : info
            .priceDV < 0 ? info.priceDV : info.priceDV
          return `<span style=color:${color};margin-left:5px>${priceDVTmp}元</span>`
        }
      }
    }]
  ]
}

function part2() {
  return [
    [{
      title: '调整后新增回款笔数',
      key: 'totalBackNum',
      text: {}
    }, {
      title: '正回款笔数',
      key: 'plusCollectionCount',
      text: {},
      data: {
        from: (info) => {
          return info.plusCollectionCount + ''
        }
      }
    }, {
      title: '正回款金额',
      key: 'plusCollectionPrice',
      text: { tooltip: false },
      data: {
        from: (info) => {
          let color = info.plusCollectionPrice > 0 ? '#D9001B' : info
            .plusCollectionPrice < 0 ?
            '#70B603' : '#515a6e'
          let plusCollectionPriceTmp = info.plusCollectionPrice > 0 ? '+' + info
            .plusCollectionPrice : info
            .plusCollectionPrice < 0 ? info.plusCollectionPrice : info
            .plusCollectionPrice
          return `<span style=color:${color};margin-left:5px>${plusCollectionPriceTmp}元</span>`
        }
      }
    }],
    [{
      title: '',
      key: '',
    }, {
      title: '负回款笔数',
      key: 'minusCollectionCount',
      text: {},
      data: {
        from: (info) => {
          return info.minusCollectionCount + ''
        }
      }
    }, {
      title: '负回款金额',
      key: 'minusCollectionPrice',
      text: { tooltip: false },
      data: {
        from: (info) => {
          let color = info.minusCollectionPrice > 0 ? '#D9001B' : info
            .minusCollectionPrice < 0 ? '#70B603' : '#515a6e'
          let minusCollectionPriceTmp = info.minusCollectionPrice > 0 ? '+' + info
            .minusCollectionPrice : info.minusCollectionPrice < 0 ? info
            .minusCollectionPrice : info.minusCollectionPrice
          return `<span style=color:${color};margin-left:5px>${minusCollectionPriceTmp}元</span>`
        }
      }
    }]

  ]
}

function part3() {
  return [
    [{
      title: '调整原因',
      key: 'adjustmentReason',
      text: {
        whiteSpace: 'normal',
        tooltip: false
      }
    }],
    [orderPic(_this.info)],
    [
      ...accessory(_this.info)
    ],
    [{
      title: '审核人',
      key: 'auditName',
      text: {}
    }, {
      title: '审核时间',
      key: 'auditTime',
      text: {
        tooltip: false
      }
    }, {
      title: '',
      key: '',
    }]
  ]
}

function options(vm) {
  _this = vm
  let end = _this.orderType == '签约' ? part2() : []
  return [
    ...part1(),
    ...end,
    ...part3()
  ]
}

export default {
  options
}